package com.mrzhou.core.tool.utils;

import com.google.common.base.Charsets;
import com.mrzhou.core.tool.constants.TokenConstant;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;

/**
 * Jwt Token工具类
 */
public class JwtUtil {

    private static final int AUTH_TOKEN_LENGTH = 7;

    public static String getToken(String auth) {
        if(!StringUtil.isNotBlank(auth) || auth.length() < AUTH_TOKEN_LENGTH) {
            return null;
        }
        // 校验是否为标准的token
        String header = auth.substring(0, 6).toLowerCase();
        if(TokenConstant.BEARER.equals(header)) {
            return auth.substring(AUTH_TOKEN_LENGTH);
        }
        return null;
    }

    public static Claims parseJwt(String authToken) {
        try {
            return Jwts.parser().setSigningKey(TokenConstant.SIGN_KEY.getBytes(Charsets.UTF_8)).parseClaimsJws(authToken).getBody();
        } catch (Exception e) {
            return null;
        }
    }

}
